#' title: "Agenda Seeding: Make Script"
#' date: "`r Sys.Date()`"
#' output: pdf_document

## ---- make_spin_code, eval = FALSE, include = FALSE ----
# spin code to output Rmd / Rnw
# set output_format to "html_document" for html
# rmarkdown::render(input = here::here("code/make_script_rep.R"), output_format = "pdf_document", clean = TRUE)

# NOTE: to generate PDF, last two code chunks need to be run
# both are currently set to eval = FALSE as they require latex and xelatex 

## ---- packages ----

library(knitr)
library(here) 

set.seed(12345678)


## ---- source_packages ----

source(here("code/source_libraries.R"))


## ---- electoral_data_prep3.R ----

## INPUT:  
#   st <- read.csv(here("data/icpsr_states_fips_census.csv"))
#   load(here("data/ICPSR_08611/dvnDataFile_509482.RData"), verbose = TRUE)
#   eg2 <- readr::read_csv(file = here("data/county_st_geocoded.csv"))


# OUTPUT: 
#   save(e8, file = here("data/electoral_4472_geocoded_panel.Rdata"))

rmarkdown::render(input = here::here("code/electoral_data_prep3.R"), 
                  output_format = "pdf_document", clean = TRUE)



## ---- demographic_data_prep3.R ----

## INPUT:  
#   county62 <- read.dta(here("data/ICPSR_02896/ICPSR_02896_0074_1962/DS0074/02896-0074-Data.dta"))
#   countynames62 <- read.table(here("data/ICPSR_02896/census1962variablenames3.txt"), sep = "\t", col.names = c("varnum", "name"), colClasses = c("character", "character") )
#   county72 <- read.dta(here("data/ICPSR_02896/ICPSR_02896_0076_1972/DS0076/02896-0076-Data.dta"))
#   countynames72 <- read.dct(here("data/ICPSR_02896/ICPSR_02896_0076_1972/DS0076/02896-0076-Setup.dct"), labels.included = "yes")
#   county83 <- read.dta(here("data/ICPSR_02896/ICPSR_02896_0078_1983/DS0078/02896-0078-Data.dta"))
#   countynames83 <- read.dct(here("data/ICPSR_02896/ICPSR_02896_0078_1983/DS0078/02896-0078-Setup.dct"), labels.included = "yes")
#   county94 <- read.dta(here("data/ICPSR_02896/ICPSR_02896_0080_1994/DS0080/02896-0080-Data.dta"))

# OUTPUT:
#   save(county3, file = here("data/county3_demo.Rdata"))

rmarkdown::render(input = here::here("code/demographic_data_prep3.R"),
                  output_format = "pdf_document", clean = TRUE)


## ---- weather_precip_station_merge3.R ----

# INPUT
#   weather <- read.csv(here("data/weather/precip_1968_04_all_stations.txt"), header = TRUE, na.strings = c("-99999", "99999") ) 
#   stations <- read.csv(here("data/weather/complete_station_list2.csv"), header = TRUE, na.strings = c("-99999", "99999") )
#   icst <- read.csv(here("data/icpsr_states_fips_census.csv"), header = TRUE)

# OUTPUT
#   save(rain, file = here("data/rainfall_geocoded.Rdata") )

rmarkdown::render(input = here::here("code/weather_precip_station_merge3.R"),
                  output_format = "pdf_document", clean = TRUE)


## ---- carter_protest_data_prep3.R ----

## INPUT:  
#   carter <- read_csv(here("data/Carter_data/carter_data.csv"))
#   st <- read.csv(here("data/icpsr_states_fips_census.csv"))
#   carter_geo <- read.csv(here("data/Carter_data/carter_geocodes.csv"), header = TRUE) 
#   r4 <- read_csv(file = here("data/Carter_data/carter_data.csv"))
#   load(file = here("data/county3_demo.Rdata"), verbose = TRUE)
#   load(file = here("data/electoral_4472_geocoded_panel.Rdata"), verbose = TRUE)
#   load(here("data/rainfall_geocoded.Rdata" ), verbose = TRUE )

## OUTPUT: 
#   save(carter_dist_matrix, file = here("data/carter_county_dist_matrix.Rdata"))
#   save(vc2, file = here("data/voting_census_rain.Rdata"))

rmarkdown::render(input = here::here("code/carter_protest_data_prep3.R"), 
                  output_format = "pdf_document", clean = TRUE)


## ---- dca_protest_prep3.R ----

## INPUT:  
#   dca_geocodes <- read_csv(file = here("data/dca_city_state_geocoded.csv"))
#   dca_orig <- foreign::read.dta(file = here("data/dynamics_of_collective_action/final_data_v10.dta"))
#   load(here("data/voting_census_rain.Rdata"), verbose = TRUE)


## OUTPUT:
#   save(protest.data, file = here::here("data/DCA_protest_data.Rdata"))
#   save(dca, file = here("data/dca_protest_data_black_geocoded.Rdata"))
#   save(dca_dist_matrix, dca_nv_dist_matrix, dca_v_dist_matrix, file = here("data/dca_county_dist_matrix.Rdata"))
#   save(vc2, file = here("data/voting_census_carter_rain_dca.Rdata"))
#   dataMaid::makeCodebook(vc2, file = here("codebooks/codebook_protest_data_combined.Rmd"), reportTitle = "Protest Data Combined", checks = list(character = NULL, factor = NULL), replace = TRUE)

rmarkdown::render(input = here::here("code/dca_protest_prep3.R"), 
                  output_format = "pdf_document", clean = TRUE)



## ---- spatial_panel_models.R ----

## INPUT:  
#   load(here("data/voting_census_carter_rain_dca.Rdata"), verbose = TRUE)
#   counties <- rgdal::readOGR(here("data/county_census/County_2010Census_DP1.shp"))

# OUTPUT: 
#   table for Appendix
#   stargazer(..., file = here("code/spatial_panel_multi_table.tex") 
#   includes coefs plm plot 
#   save(pout_nv, pout_v, pout_v2, sp_nv, sp_v, sp_v2, sp_coef, sp_se, file = here("data/spatial_panel_multi2.Rdata"))

rmarkdown::render(input = here::here("code/spatial_panel_models3.R"),
                  output_format = "pdf_document", clean = TRUE)




## ---- APSR_protests3_1_rep.R ----

# FORMAT: 
# code_chunk_header 
#    followed by read/load/source/save/ command with data in/out

# load_packages
#   source(here("code/source_libraries.R"))

# load_processed_protest_data_updated
#   load(here("data/voting_census_carter_rain_dca.Rdata"), verbose = TRUE)

# mostimportantproblem1950
#   polls <- read.csv(here("data/polls1950-1979-date.csv"))

# plm_calc_load_data, eval = FALSE
#   load(here("data/county3_demo.Rdata"), verbose = TRUE)

# plm_models
#   save(pdata_nv, pdata_v, pdata_v2, file = here("data/pdata.Rdata"))

# plm_calc_save
#   save(coefspan_v, sespan_v, coefspan_v2, sespan_v2, file = here("data/plm.coefs.Rdata") )

# plm_matching
#   fips_codes <- read.csv(here("data/icpsr_states_fips_census.csv")) %>% clean_names()

# summary.stats.plm
#   save(summary_nv64, summary_v68, summary_v268, file = here("data/summary_stats.Rdata"))
#   save(summary_nv64_unmatched, summary_v68_unmatched, summary_v268_unmatched, file = here("data/summary_stats_unmatched.Rdata"))

# load_spatial_panel_models
#   load(here("data/spatial_panel_multi2.Rdata") )

# rainfall_instrument_load_data, eval = FALSE
#   load(here::here("data/x9large_mlk_2new_04_20_2014.Rdata"), verbose = TRUE )
#   load(here("data/county_rainfall_orig.Rdata"), verbose = TRUE )

# load_vc2
#   load(here("data/voting_census_carter_rain_dca.Rdata"), verbose = TRUE)

# mlk_counterfactual_sim_load_data
#   y68vote  <- read.csv(here("data/pres68sdetailedresults3.csv"), header = TRUE) 
#   st.codes <- read.csv(here("data/icpsr_states_fips_census.csv") )

# counterfactual_summary
#   save(evs.table, file = here("data/evs_table.Rdata") )

# load_all_data
#   load(here("data/all_data_attica_congress_no_nyt.Rdata") )

# congress_riots_violent_protests_plot_load_data 
#   load(here("data/congress_counts3_new.Rdata"), verbose = TRUE )
#   riots <- read.delim(here("data/Carter_data/carter_data_raw.txt"), sep=" ") 

# framing_lda_analysis_load_data
#   load(here("data/newspapers_random_subsample_all_words_no_nyt.Rdata") )
#   load(here("data/newspapers_random_subsample_article_doc_topic.Rdata") )

# load_dca_for_nyt_models
#   load(here("data/DCA_protest_data.Rdata"))

# framing_case_2_vs_3_load_data
#   load(here("data/framing-data-random_subsample_all_words_no_nyt_four_cases.Rdata"))

# granger_calc_load_data 
#   load(here("data/congress_counts3_new.Rdata") )
#   polls.gr <- read.csv(here("data/polls1950-1979-date.csv") )
#   riots <- read.delim(here("data/Carter_data/riot3.txt"), sep=" ")
#   load(here("data/DCA_protest_data.Rdata"), verbose=FALSE )

# load_data_granger 
#   load(here("data/all_data_attica_congress_no_nyt.Rdata") )

# protestsbyyear
#   load(here("data/DCA_protest_data.Rdata"), verbose = TRUE )

# load_pdata1
#   load(here("data/voting_census_carter_rain_dca.Rdata"), verbose = TRUE)

# usnonviolentprotestpublicopinion1960plot
#   load(here("data/DCA_protest_data.Rdata") )

# usviolentprotestspublicopinion1964plot
#   polls <- read.csv(here("data/polls1950-1979-date.csv") )
#   riots <- read.delim(here("data/Carter_data/carter_data_raw.txt"), sep=" ")
#   load(here("data/DCA_protest_data.Rdata") )

# congress_rights_nonviolent_protests_plot
#   load(here("data/DCA_protest_data.Rdata") )


# blackpartyid
#   pid <- read.csv(here("data/Bositis_Party_Identification/blackpartyid1936_2012.csv") )

# cointegration_setup
#   load(here("data/all_data_attica_congress_no_nyt.Rdata"))

# sweep_nv_cumulative
#   load(here("data/sweep_nv_cumulative.Rdata"), verbose = FALSE )

# sweep_v_cumulative
#   load(here("data/sweep_v_cumulative.Rdata"), verbose = FALSE ) 

# sweep_v2_cumulative
#   load(here("data/sweep_v2_cumulative.Rdata"), verbose = FALSE ) 

# sweep_nv_binary
#   load(here("data/sweep_nv_binary.Rdata"), verbose = FALSE )

# sweep_v_binary
#   load(here("data/sweep_v_binary.Rdata"), verbose = FALSE ) 

# sweep_v2_binary
#   load(here("data/sweep_v2_binary.Rdata"), verbose = FALSE ) 


rmarkdown::render(
  input         = here::here("code/APSR_protests3_1_rep.R"),
  output_format = "html_document", 
  clean         = TRUE
  )




## ---- summary_stats_revised ----

# summary_stats.Rdata generated by main doc

# input:  data/summary_stats.Rdata 
# input:  data/summary_stats_unmatched.Rdata
# input:  code/summary_stats_revised.tex  # generated by knitting R file

# \input into main doc
# to generate .tex files below, knit summary_stats_revised.R once then
# knit summary_stats_revised_extract.R
#
# output: docs/summary_stats_revised_extract_unmatched_full_table.tex
# output: docs/summary_stats_revised_extract_matched.tex 
# output: docs/summary_stats_revised_extract_unmatched.tex


rmarkdown::render(
  input         = here::here("code/summary_stats_revised.R"),
  output_format = "pdf_document",
  clean         = TRUE
  )


rmarkdown::render(
  input         = here::here("code/summary_stats_revised_extract.R"), 
  output_format = "pdf_document", 
  clean         = TRUE
  )


## ---- APSR_protests3_1_rep.Rnw, eval = FALSE ----

# knit Rnw to tex in first stage, convert tex to pdf in second stage 

# to ensure figure and cache directories are in code directory
# temporarily set working directory to code
setwd(here("code"))

# knit to .tex output with R Sweave
knitr::knit(input  = here::here("code/APSR_protests3_1_rep.Rnw"), 
            output = here::here("code/APSR_protests3_1_rep.tex"),
            quiet  = FALSE)

# restore to here()
setwd(here::here())

## ---- APSR_protests3_1_rep.tex, eval = FALSE ----

# compile .tex to pdf with system call (requires latex and xelatex)

# for system call, need to hard-set directory so other files
# like apsr_submission.cls are in right relative path
setwd(here("code"))
file_name <- "APSR_protests3_1_rep.tex"

system(command = paste0("latexmk -xelatex ", file_name),
       timeout = 60) # helps prevent external call from freezing R

setwd(here::here())
